import SwiftUI

struct SettingsView: View {
    @Binding var standingDuration: Double
    @Binding var sittingDuration: Double
    @Environment(\.dismiss) private var dismiss
    let timeManager: TimeManager
    
    @State private var tempStandingDuration: Double
    @State private var tempSittingDuration: Double
    
    init(standingDuration: Binding<Double>, sittingDuration: Binding<Double>, timeManager: TimeManager) {
        self._standingDuration = standingDuration
        self._sittingDuration = sittingDuration
        self.timeManager = timeManager
        self._tempStandingDuration = State(initialValue: standingDuration.wrappedValue)
        self._tempSittingDuration = State(initialValue: sittingDuration.wrappedValue)
    }
    
    var body: some View {
        VStack(spacing: 30) {
            Text("时长设置")
                .font(.title)
                .padding(.top)
            
            VStack(alignment: .leading, spacing: 20) {
                DurationSettingRow(title: "站立时长",
                                 duration: $tempStandingDuration,
                                 color: PostureType.standing.color)
                
                DurationSettingRow(title: "坐姿时长",
                                 duration: $tempSittingDuration,
                                 color: PostureType.sitting.color)
            }
            .padding(.horizontal)
            
            HStack(spacing: 20) {
                Button("取消") {
                    dismiss()
                }
                .keyboardShortcut(.escape)
                
                Button("确定") {
                    standingDuration = tempStandingDuration
                    sittingDuration = tempSittingDuration
                    timeManager.updateDurations(standing: tempStandingDuration, sitting: tempSittingDuration)
                    dismiss()
                }
            }
            .padding()
            
            Spacer()
        }
        .frame(width: 300, height: 300)
    }
}

struct DurationSettingRow: View {
    let title: String
    @Binding var duration: Double
    let color: Color
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text(title)
                .foregroundColor(color)
            
            HStack {
                Slider(value: $duration, in: 1...120, step: 1)
                Text("\(Int(duration))分钟")
                    .monospacedDigit()
                    .frame(width: 60, alignment: .trailing)
            }
        }
    }
} 